﻿using Canteen.Core.Models;
using Canteen.Core.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace Canteen.Core.WebApp.API.Controllers
{
    /// <summary>
    /// 订单控制器
    /// </summary>
    [Route("shoporder")]
    [ApiController, Authorize]
    public class ShopOrderController : ControllerBase
    {
        #region 构造函数
        private readonly IShopOrderService shopOrderService;
        private readonly IUserService userService;
        public ShopOrderController(IShopOrderService shopOrderService, IUserService userService)
        {
            this.shopOrderService = shopOrderService ?? throw new ArgumentNullException(nameof(shopOrderService));
            this.userService = userService ?? throw new ArgumentNullException(nameof(userService));
        }
        #endregion

        // 终端 提交订单
        // POST /shoporder
        [HttpPost]
        public async Task<HttpResult<bool>> Post([FromBody] ShopOrderSubmitDTO value)
        {
            return HttpResult<bool>.Success(await shopOrderService.CreateShopOrder(value));
        }
        //GET: /shoporder/pagelist
        [HttpGet("PageList")]
        public async Task<HttpResult<dynamic>> GetPageList([FromQuery] ShopOrderPageParameters parameters)
        {
            parameters.UserId = userService.UserId;
            return HttpResult<dynamic>.Success(await shopOrderService.GetPageList(parameters));
        }
        //GET: /shoporder/homeechart
        [HttpGet("HomeEchart")]
        public async Task<HttpResult<HomeEchartDTO>> GetHomeEchart()
        {
            return HttpResult<HomeEchartDTO>.Success(await shopOrderService.GetHomeEchart(userService.UserId));
        }

        // GET /shoporder/5
        [HttpGet("{id}")]
        public async Task<HttpResult<dynamic>> Get(int id)
        {
            return HttpResult<dynamic>.Success(await shopOrderService.SelectShopOrderById(id));
        }
    }
}
